package com.example.blogdemo.service;

import com.example.blogdemo.entity.LoginTicket;
import com.example.blogdemo.entity.User;
import com.example.blogdemo.repository.LoginTicketRepository;
import com.example.blogdemo.repository.UserRepository;
import com.example.blogdemo.util.JblogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @Author Anita
 * @Date 2017/12/13 11:57
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private LoginTicketRepository loginTicketRepository;

    public User getUser(Integer userId){
        return userRepository.findById(userId);
    }

    public User getUserByName(String name){
        return userRepository.findByName(name);
    }

    public void addUser(User user){
        userRepository.save(user);
    }

    public Map<String,String> register(String username, String password){
        Map<String,String> map = new HashMap<>();
        Random random = new Random();
        if(StringUtils.isEmpty(username)){
            map.put("msg","用户名不能为空");
            return map;
        }
        if(StringUtils.isEmpty(password)){
            map.put("msg","密码不能为空");
            return map;
        }
        User isUser = userRepository.findByName(username);
        if(isUser!=null){
            map.put("msg","用户名已被使用");
            return map;
        }
        User user = new User();
        user.setName(username);
        user.setSalt(UUID.randomUUID().toString().substring(0,5));
        user.setHeadUrl("http://img4.imgtn.bdimg.com/it/u=3974294345,2548987431&fm=27&gp=0.jpg");
        user.setPassword(JblogUtil.MD5(password+user.getSalt()));
        user.setRole("admin");
        userRepository.save(user);
        String ticket = addLoginTicket(user.getId());
        map.put("ticket",ticket);
        return map;
    }

    public Map<String,String> login(String username,String password){
        Map<String,String> map = new HashMap<>();
        if(StringUtils.isEmpty(username)){
            map.put("msg","用户名不能为空");
            return map;
        }
        if(StringUtils.isEmpty(password)){
            map.put("msg","密码不能为空");
            return map;
        }
        User user = userRepository.findByName(username);
        if(user == null){
            map.put("msg","用户名不存在");
            return map;
        }
        if(!JblogUtil.MD5(password+user.getSalt()).equals(user.getPassword())){
            map.put("msg","密码错误");
            return map;
        }

        String ticket = addLoginTicket(user.getId());
        map.put("ticket",ticket);
        return map;

    }

    public void logout(String ticket){
        loginTicketRepository.updateStatusByTicket(1,ticket);
    }

    public String addLoginTicket(Integer userId){
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUserId(userId);
        Date date = new Date();
        date.setTime(date.getTime()+1000*3600*30);
        loginTicket.setExpired(date);
        loginTicket.setStatus(0);
        loginTicket.setTicket(UUID.randomUUID().toString().replaceAll("-",""));
        loginTicketRepository.save(loginTicket);
        return loginTicket.getTicket();
    }




}
